package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionHiddenService;
import com.bidevalution.support.context.Constants;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author huyiqi
 * @date 2020/4/15
 */
@RestController
@RequestMapping("/beSectionHidden")
@Api(description = "暗标相关Controller")
public class BeSectionHiddenController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BeSectionHiddenController.class);

    @ApiOperation(value = "打印暗标关联表之前的校验", notes = "打印暗标关联表之前的校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping(value = "/printPdfCheck")
    public Response printPdfCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf;charset=UTF-8");
        String sectionId = request.getParameter("sectionId");
        String password = request.getParameter("password");
                String userGuid = request.getHeader("userGuid");
        TSUser user = RedisAccess.getValue(TSUser.class, userGuid);
        String userLoginName = null;
        if(user!=null) {
            userLoginName = user.getLoginName();
        }
        try {
            return beSectionHiddenService.printPdfCheck(sectionId, userLoginName, password);
        } catch (Exception e) {
            log.error("打印暗标关联表，校验时发生错误", e);
            return Response.fail("校验失败");
        }
    }


    @ApiOperation(value = "打印暗标关联表", notes = "打印暗标关联表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "授权码", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping(value = "/printPdf")
    public void printPdf(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf;charset=UTF-8");
        String sectionId = request.getParameter("sectionId");
        String token = request.getParameter("token");
        log.info("==========printPdf==========打印暗标关联表==========token："+token);
        token = token.replaceAll(" ", "+");
        OutputStream os = response.getOutputStream();
        try {
            beSectionHiddenService.printHidden(sectionId, token, os);
        } catch (Exception e) {
            log.error("打印暗标关联表时发生错误", e);
        }
    }

    @ApiOperation(value = "打印暗标关联表的按钮状态", notes = "打印暗标关联表的按钮状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping(value = "/getButtonStatus")
    public Response getButtonStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf;charset=UTF-8");
        String sectionId = request.getParameter("sectionId");
        try {
            return beSectionHiddenService.getButtonStatus(sectionId);
        } catch (Exception e) {
            log.error("获取打印暗标关联表按钮状态时出错", e);
            return Response.ok(Constants.STR_NO);
        }
    }


    @Autowired
    BeSectionHiddenService beSectionHiddenService;
}
